// 一系列自定义的的PopUp

// 按钮事件;
function iconDefault(el){
  var icon = el.find("use");
  var checkStatus = icon.attr("xlink:href");
  if(checkStatus !== "#icon-checked"){
    if (checkStatus === "#icon-uncheck") {
      icon.attr("xlink:href","#icon-check").parent().addClass("checked");
    }else if (checkStatus === "#icon-check"){
      icon.attr("xlink:href","#icon-uncheck").parent().removeClass("checked");
    }
  }
}

// ******************************其他银行PopUp*******************
var blanksData = [{active: false,icon: "#icon-cashier-jsyh",title: "建设银行",visaSupport: true},{active: false,icon: "#icon-cashier-jsyh",title: "建设银行",description: {"cardNum": "信用卡","cardTips": "周末及9号龙支付最高立减150"},visaSupport: true}, {active: false,icon: "#icon-cashier-gsyh",title: "广州银行",description: {"cardNum": "电话卡","cardTips": "什么鬼东西"},visaSupport: true}, {active: false,icon: "#icon-cashier-jsyh",title: "你好银行",description: {"cardTips": "充值能够使你变强"},visaSupport: true}, {active: false,icon: "#icon-cashier-gsyh",title: "工商银行",description: {"cardNum": "无限流量卡",},visaSupport: true}];
      
function openOtherBlanks(){

  function otherBlanksTpl(){
    // 判定index;
    var actvieIndex = $(".cashier-page").find(".payWayList .blankItem").attr("blandindex");
    // 判定是否同时在激活状态
    var activeStatus = !!$(".cashier-page").find(".payWayList .blankItem").find(".contact-check").find(".checked").length
    // ul外层
    var tplContent ='<ul class="payWayList">';
    // 定义内部itemTpl
    function createItemBlank(item,status) {
      return '<li class="gzl-cell payWayItem" blandindex blankItem>' +
        '              <div class="gzl-cell__hd">' +
        '                  <svg class="icon" aria-hidden="true">' +
        '                    <use xlink:href="' + item.icon + '"></use>' +
        '                  </svg>' +
        '              </div>' +
        '              <div class="gzl-cell__bd">' +
        '                <div class="payWayText-up">' + item.title + '</div>' +
        (item.description ? '<div class="payWayText-down">' + (item.description.cardNum ? '<span class="cardNum">' + item.description.cardNum + '</span>' : '') + (item.description.cardTips ? '<span class="cardTips">' + item.description.cardTips + '</span>' : '') + '</div></div>' : '') +'</div>'+
        '              <div class="contact-check">' +
        '                  <svg class="icon '+(status?"checked":'')+'" aria-hidden="true">' +
        '                    <use xlink:href="'+(status?"#icon-check":'#icon-uncheck')+'"></use>' +
        '                  </svg>' +
        '              </div>' +
        '            </li>';
    }; 
    for(var i=0;i<blanksData.length;i++){
      if(activeStatus && actvieIndex == i){
        tplContent += createItemBlank(blanksData[i],true);
      }else{
        tplContent += createItemBlank(blanksData[i]);
      }
    }
    tplContent = tplContent+ '</ul>'
    return tplContent
  }

  

  $.popup({
    content: otherBlanksTpl(),
    insideClass: "otherBlanksPopUp",
    title: '选择银行卡',
    insideTplEvent: function () {
      // 按钮事件
      $(".otherBlanksPopUp").find(".payWayItem").on("click",function(){
        var cashierPayWayList = $(".cashier-page").find(".payWayList");
        //  获得取的index;
        var blankIndex = $(this).index();
        iconDefault($(this).find(".contact-check"));
        var clickItemTpl = $(this).html();
        // 移除外层所有check;
        cashierPayWayList.find(".payWayItem").find(".contact-check use").attr("xlink:href","#icon-uncheck").parent().removeClass("checked");
        // 首赋游 收起;
        $(".dividedPay").slideUp(300);
        // 覆盖html 且赋值index 状态;
        cashierPayWayList.find(".blankItem").html(clickItemTpl).attr("blandindex",blankIndex);
        // 
        $.hidePopup();
      })
    }
  });
}


// ******************************促销PopUp*******************


var promotionData = [{title: "满赠优惠",promotionSelectItem: [{defaultChecked:false,rulesName: "元旦促销满减元元旦促销满减元旦促销满1000立旦促销满1000立",moneyReduce: 800,rulesDescription: "描述文字两端强制对齐1， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐。"}]},{title: "早鸟优惠",promotionSelectItem: [{defaultChecked:true,rulesName: "买买买",moneyReduce: 800,rulesDescription: "描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐。"},{defaultChecked:false,rulesName: "幸运360",moneyReduce: 1800,rulesDescription: "描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐。"},{defaultChecked:false,rulesName: "元旦好开心",moneyReduce: 080,rulesDescription: "描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐。"}]},{title: "其他大优惠",promotionSelectItem: [{defaultChecked:false,rulesName: "元促销满1000立",moneyReduce: 800,rulesDescription: "描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐， 描述文字两端强制对齐。"}]}]

function promotionPopUp(){

  var siumulateContent = _.template($("#promotionPopUp").html())(promotionData)
  $.popup({
    content: siumulateContent,
    insideClass: "promotionPopUp",
    title: '促销',
    btn: '按钮',
    insideTplEvent: function () {
      // 选择按钮
      var selectItem = $(".promotionPopUp").find(".promitionSelectItem");
      selectItem.find(".contact-check").on("click",function(){
        iconDefault($(this));
      })
      // 隐藏事件;
      $(".promotionPopUp").find(".gzl-popup__ft").on("click",function(){
        $.hidePopup();
      })
      // 查看促销规则
      $(".showRulesDetail").on("click",function(){
        $(this).parents(".promitionSelectItem").find(".rulesDetail").toggleClass("active");
      })
    }
  });
}



// ******************************邮轮更换日期*******************


function cruiseProdChangeDate(self){
  var selectIndex = self.parent().attr("selectIndex");
  $.popup({
    title: '出发日期',
    titleAlign: 'center',
    line: false,
    content: _.template($("#more-cruiseDate-select").html())(cruiseDate),
    insideTplEvent:function(){
      $(".more-cruise-date-selectPop").find(".gzl-cell").on("click",function(){
        var index = $(this).index();
        var text = $(this).find(".date").text().split(" ")[0].replace(/-/g, "/");
        var weekDay = new Date(text).getDay();
        var chineseStr = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
        text = text +" " + chineseStr[weekDay];
        console.log(weekDay);
        $(".cruise-date-select").attr("selectIndex",index);
        $(".cruise-date-select .gzl-flex__item").text(text);
        $.hidePopup();
      })
    }
  });

  $(".more-cruise-date-selectPop").find(".gzl-cell").eq(selectIndex).addClass("active");
}

// ****************************** 邮轮choose页选择数量 *******************

// 选择入住人数
function cruiseChooseMen(self){
  var clickItem = self;
  $.popup({
    title: '选择入住人数',
    titleAlign: 'left',
    line: true,
    insideClass: "selectCruiseMenber",
    content: $("#cruiseChoosePop").html(),
    insideTplEvent:function(){
      // 获取状态;
      var adultNum = clickItem.attr("adult") | 0;
      var childNum = clickItem.attr("child") | 0;
      var roomNum = clickItem.attr("total") | 0;
      // 赋值
      $(".cruiseChooseMen-wrap").find(".adult .gzl-count__num").text(adultNum);
      $(".cruiseChooseMen-wrap").find(".child .gzl-count__num").text(childNum);
      $(".cruiseChooseMen-wrap").find(".total .roomNum span").text(roomNum);


      //todo Something
      // 加减点击事件
      $(".gzl-count .gzl-count__minus, .gzl-count .gzl-count__plus").on("click",function(){
        var value;
        if($(this).hasClass("gzl-count__minus"))
          value = -1;
        else if($(this).hasClass("gzl-count__plus"))
          value = 1;
        
        // 随便写逻辑
        if(value == 1){
          $(".cruiseChooseMen-wrap").find(".roomNum").find("span").text(2);
        }else{
          $(".cruiseChooseMen-wrap").find(".roomNum").find("span").text(0);
        }

        var numDom = $(this).parent().find(".gzl-count__num");
        var val = parseInt(numDom.text()) +value;
        
        //  随便写的
        if(val>-1){
          numDom.text(val);
        }

      })
      // 确定赋值;
      $(".selectCruiseMenber .gzl-footbar").on("click",function(){
        var adultNum = $(".cruiseChooseMen-wrap").find(".adult .gzl-count__num").text();
        var childNum = $(".cruiseChooseMen-wrap").find(".child .gzl-count__num").text();
        var roomNum = $(".cruiseChooseMen-wrap").find(".total .roomNum span").text();
        clickItem.attr("adult",adultNum);
        clickItem.attr("child",childNum);
        clickItem.attr("total",roomNum);
        if(parseInt(roomNum) >0){
          clickItem.parent().find(".cabinNum").show();
          // 此处应该添加计算过程;
          clickItem.parents(".gzl-cells").find(".gzl-cell .gzl-cell__bd span").css("display","inline-block")
          clickItem.addClass("revise");
        }else{
          clickItem.parent().find(".cabinNum").hide();
          clickItem.parents(".gzl-cells").find(".gzl-cell .gzl-cell__bd span").css("display","none")
          clickItem.removeClass("revise");
        }

        $.hidePopup();
      })
    }
  });
}
